package com.csk.zsk.compents.action;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csk.zsk.commons.action.AjaxActionSupport;
import com.csk.zsk.commons.utils.encode.JsonBinder;
import com.csk.zsk.commons.utils.web.struts2.Struts2Utils;
import com.csk.zsk.compents.entity.Comment;
import com.csk.zsk.compents.entity.User;
import com.csk.zsk.compents.service.CommentService;
import com.csk.zsk.compents.service.UserService;
import com.csk.zsk.compents.utils.Constants;

@Controller
@Scope("prototype")
public class CommentAction extends AjaxActionSupport {

	private static final long serialVersionUID = -4852774213280133952L;
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();
	@Autowired
	private CommentService commentService;
	@Autowired
	private UserService userService;
	private String id;
	private String userid;
	private String objid;
	private String commentid;
	private boolean hasSub;
	private String content;
	
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getObjid() {
		return objid;
	}
	public void setObjid(String objid) {
		this.objid = objid;
	}
	public String getCommentid() {
		return commentid;
	}
	public void setCommentid(String commentid) {
		this.commentid = commentid;
	}
	public boolean isHasSub() {
		return hasSub;
	}
	public void setHasSub(boolean hasSub) {
		this.hasSub = hasSub;
	} 
	
	public CommentAction(){
		binder.setDateFormat("yyyy年,MM月dd日,HH:mm");
	}
	
	public void getSubCommentListById(){
		List<Comment> list = commentService.getSubCommentListById(commentid);
		String json = binder.toJson(getUsernameList(list));
		Struts2Utils.renderJson(json);
		
	}
	
	public void getCommentListByOjbid(){
		List<Comment> list = commentService.getCommentListByOjbid(objid, hasSub);
		String json = binder.toJson(getUsernameList(list));
		Struts2Utils.renderJson(json);
	}
	
	public void addComment(){
		if (userid == null || userid.equals("")) {
			User user = (User) Struts2Utils.getSessionAttribute(Constants.USER_SESSION);
			userid = user.getId();

		}
		Comment comment = new Comment();
		comment.setObjid(objid);
		comment.setUserid(userid);
		comment.setCreatetime(new Date());
		comment.setUpdatetime(new Date());
		comment.setContent(content);
		comment.setCommentid(commentid);
		
		commentService.saveComment(comment);
		String json = binder.toJson(comment);
		Struts2Utils.renderJson(json);
		
	}
	
	private List<Comment> getUsernameList(List<Comment> list){
		for(Comment comment:list){
			String userid = comment.getUserid();
			User user = userService.getUser(userid);
			comment.setUsername(user.getRealName());
			comment.setUserimgpath(user.getPhoto());
		}
		
		return list;
	}
	
	
	
}
